package com.qiao.dfw.loggic.hall;

import com.qiao.dfw.config.SpringBeanUtil;
import com.qiao.dfw.dao.PlayerMapper;
import com.qiao.dfw.loggic.ECode;
import com.qiao.dfw.loggic.HallTask;
import com.qiao.dfw.manager.LoggerManager;
import com.qiao.dfw.proto.Login;
import com.qiao.dfw.thread.EThreadType;
import com.qiao.dfw.thread.ThreadManager;

/**
 * @Author qzj
 * @Data 2023/9/21 10:45
 * @Description
 * @Version 1.0
 */
public class UpdateNickname extends HallTask<Login.UserUpdateNicknameRequest> {
    @Override
    public void execute0() {
        LoggerManager.info("UpdateNickname");
        PlayerMapper mapper = SpringBeanUtil.getBean(PlayerMapper.class);

        // 启动服务器的时候,缓存map 以及 全部用户昵称
        int count = mapper.getNickname(request.getNickname());
        Login.UserUpdateNicknameResponse.Builder builder = Login.UserUpdateNicknameResponse.newBuilder();
        if (count > 0) {
            builder.setSuccess(false);
            builder.setMsg("昵称被占用");
            sendMsg(builder.build().toByteArray());
        }else {
            player.nickname = request.getNickname();
            mapper.update(player);
            builder.setSuccess(true);
            builder.setNickname(request.getNickname());
            sendMsg(builder.build().toByteArray());
        }
    }

    @Override
    public ECode getCode() {
        return ECode.UpdateNickname;
    }
}
